package com.tekj.cxqcb.presenter.main2;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tekj.cxqcb.R;
import com.nineoldandroids.view.ViewHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import common.widget.draglayout.DragLayout;
import es.dmoral.toasty.Toasty;

/**
 * description
 * Created by JustTong on 2018/11/29.
 */
public class Main2PresenterImpl implements Main2Presenter {

    // 设置退出时长
    private long mExitTime;
    private Fragment currentShowFragment;//当前显示的模块
    private Activity mActivity;

    public Main2PresenterImpl(Activity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public void onExit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toasty.normal(mActivity, "再按一次退出程序").show();
            mExitTime = System.currentTimeMillis();
        } else {
            mActivity.finish();
        }
    }

    @Override
    public void setStatusBar(final View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            final int statusHeight = getStatusBarHeight(mActivity);
            view.post(new Runnable() {
                @Override
                public void run() {
                    int titleHeight = view.getHeight();
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                    params.height = statusHeight + titleHeight;
                    view.setLayoutParams(params);
                }
            });
        }
    }

    @Override
    public void initDragLayout(DragLayout dl, final View view) {
        dl.setDragListener(new DragLayout.DragListener() {
            //界面打开的时候
            @Override
            public void onOpen() {
            }

            //界面关闭的时候
            @Override
            public void onClose() {
            }

            //界面滑动的时候
            @Override
            public void onDrag(float percent) {
                ViewHelper.setAlpha(view, 1 - percent);
            }
        });
    }

    @Override
    public void initTab(TextView[] tvIco) {
        Typeface iconfont = Typeface.createFromAsset(mActivity.getAssets(), "iconfont/iconfont.ttf");
        for (int i = 0; i < tvIco.length; i++) {
            tvIco[i].setTypeface(iconfont);
            tvIco[i].setTextSize(20);
        }
    }

    @Override
    public void initTabColor(TextView[] tvIco, TextView[] tvName, int index) {
        for (int i = 0; i < tvIco.length; i++) {
            if (i == index) {
                tvIco[i].setTextColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
                tvName[i].setTextColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
            } else {
                tvIco[i].setTextColor(ContextCompat.getColor(mActivity, R.color.tab_text_color));
                tvName[i].setTextColor(ContextCompat.getColor(mActivity, R.color.tab_text_color));
            }

        }
    }

    @Override
    public List<Fragment> initFragment(FragmentManager fragmentManager) {
        List<Fragment> fragmentList = new ArrayList();
        //根据实际情况进行调整添加

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        for (Fragment fragment : fragmentList) {
            transaction.add(R.id.main_info_fragment, fragment);
            transaction.hide(fragment);
        }
//        transaction.commit();
        transaction.commitAllowingStateLoss();
        return fragmentList;
    }


    /**
     * 获取状态栏的高度
     *
     * @return
     */
    protected int getStatusBarHeight(Activity m) {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return m.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
